﻿using Microsoft.International.Converters.PinYinConverter;
using System.Linq;
using System.Text;

namespace Basefr.Runtime.Helpers
{
    public static class PinYinHelper
    {
        public static string GetInitial(string chinese)
        {
            if (string.IsNullOrWhiteSpace(chinese))
            {
                return chinese;
            }

            var stringBuilder = new StringBuilder();

            chinese.ToCharArray().ToList().ForEach(c =>
            {
                var pinYin = NPinyin.Pinyin.GetPinyin(c);

                if (ChineseChar.IsValidChar(pinYin[0]))
                {
                    pinYin = new ChineseChar(c).Pinyins?.FirstOrDefault();
                }

                stringBuilder.Append(pinYin?.First());
            });

            return stringBuilder.ToString();
        }

        public static string GetFullPinYin(string chinese)
        {
            if (string.IsNullOrEmpty(chinese))
            {
                return chinese;
            }

            var stringBuilder = new StringBuilder();

            chinese.ToCharArray().ToList().ForEach(c =>
            {
                var pinYin = NPinyin.Pinyin.GetPinyin(c);
                //if (ChineseChar.IsValidChar(pinYin[0]))
                //{
                //pinYin = new ChineseChar(c).Pinyins?.FirstOrDefault();
                //}

                stringBuilder.Append(pinYin ?? "");
            });

            return stringBuilder.ToString();
        }

    }
}
